package com.doumi.biz.qadev.api.testcase.commonfilter;

import com.doumi.biz.qadev.api.methods.OperationRedis;
import com.doumi.biz.qadev.api.testcase.RequestTemplate;
import io.restassured.path.json.JsonPath;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * Created by YDZ on 2019/11/20.
 */
public class CommonFilterCommunicationRecord {
    private RequestTemplate requestTemplate = new RequestTemplate();
    private OperationRedis operationRedis = new OperationRedis();
    private static List<String> cuIds = new ArrayList<>();

    //redis初始化数据
    @BeforeClass(description = "初始化redis数据，redis中的数据为备注信息为空号过期等信息的用户，450638181用户信息为正常用户信息")
    public  void operationData(){
        String filenanme = "src/source/redisfile/filter-communicationrecord.csv";
        String mothodname = "communicationRecord";
        cuIds = operationRedis.readCsvoperationRedis(filenanme,mothodname,4);
    }
    @Test(description = "接口测试中，用户信息共有5个用户，其中前四个用户为有备注信息为空号、不考虑不需要、过期，一个号码为正常数据，，预期返回一条数据，并且返回值为450638181，code状态码是否为0，msg信息是否为success")
    public void commentInformationFilterRemainingOne(){
        StringBuffer sb = new StringBuffer("[");
        for (int i = 0; i <cuIds.size()-1;  i++) {
            sb.append(cuIds.get(i).trim()).append(",");
        }
        sb.append(cuIds.get(cuIds.size()-1).trim());
        sb.append("]");
        HashMap hashMap = new HashMap();
        hashMap.put("cuIds",sb.toString());
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String msg = jsonPath.getString("msg");
        //校验每个参数
        List<String> diffUserid = new ArrayList<String>(resurlt);
        for (String userid: cuIds ) {
            for (Object aResurlt : resurlt) {
                if (Objects.equals(String.valueOf(aResurlt),String.valueOf(userid))) {
                    diffUserid.remove(aResurlt);
                    break;
                }
            }
        }
        System.out.println(diffUserid);
        Reporter.log("有被过滤掉的数据，具体的userid:"+diffUserid);
        Assert.assertEquals(diffUserid.size(),0);
        Assert.assertEquals(String.valueOf(resurlt.get(0)),String.valueOf(450638181));
        Assert.assertEquals(msg,"success");
    }

    @Test(description = "请求参数cuIds为一个用户450638177,用户备注信息为空号，返回数据为空")
    public void checkCommentSpaceFilter(){
        HashMap hashMap = new HashMap();
        hashMap.put("cuIds","450638177");
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String code = jsonPath.getString("code");
        Assert.assertEquals(resurlt.size(),0);
        Assert.assertEquals(code,"200");
    }
    @Test(description = "请求参数cuIds为两个用户,用户备注信息为过期、不考虑，返回数据为空，返回code值为0，msg为success")
    public void checkCommentIllegalityFilter(){
        HashMap hashMap = new HashMap();
        String cuIds="[450638178,450638179]";
        hashMap.put("cuIds",cuIds);
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String code = jsonPath.getString("code");
        Assert.assertEquals(resurlt.size(),0);
        Assert.assertEquals(jsonPath.getString("msg"),"success");
        Assert.assertEquals(code,"200");
    }
    //删除数据
    @AfterClass(description = "删除出入掉的数据，避免影响其他测试")
    public void removeData(){
        operationRedis.removeRedis(cuIds);
    }
}
